package com.register.dao;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.register.beans.RegisterBean;

/**
 * Servlet implementation class MemberRegisterServlet
 */
public class MemberLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RegisterDAO registerDAO = null;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String errMsg = "아이디 또는 비밀번호를 다시 확인하세요.";
		String userid = (String) request.getParameter("userid");
		String passwd = (String) request.getParameter("passwd");

		registerDAO = new RegisterDAO();
		RegisterBean memberInfo = registerDAO.searchMember(userid.toLowerCase(), passwd);
		request.setAttribute("memberInfo", memberInfo);

		if (checkMember(memberInfo, userid, passwd)) {
			request.getRequestDispatcher("ajax_memberInfo.forrest").forward(request, response);
		} else {
			request.setAttribute("errMsg", errMsg);
			request.getRequestDispatcher("logIn.forrest").forward(request, response);
		}

	}

	private boolean checkMember(RegisterBean memberInfo, String userid, String passwd) {
		return userid.equals(memberInfo.getId()) && passwd.equals(memberInfo.getPassWd()) ? true : false;
	}

}
